package com.learn.mq.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.learn.mq.beans.Person;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * ClassName:MessageConverter
 * Package:com.learn.mq.util
 * Description:
 *
 * @Author:@郭先森
 * @Create:2025/7/30 - 8:06
 * @Version:v1.0
 */
public class MessageConverter {
    public static Message toRocketMessage(String topic,String tags,Object payload){
        try {
            byte[] body = JSON.toJSONString(payload).getBytes(StandardCharsets.UTF_8);
            return new Message(topic,tags,body);
        }catch (Exception e){
            throw new RuntimeException("消息转换失败",e);
        }

    }

    public static Message toRocketMessage(String topic,Object payload){
        try {
            byte[] body = JSON.toJSONString(payload).getBytes(StandardCharsets.UTF_8);
            return new Message(topic,body);
        }catch (Exception e){
            throw new RuntimeException("消息转换失败",e);
        }
    }
}

